<template>
  <view class="recruit-filter">
    <view class="recruit-filter__header">
      <text class="recruit-filter__title">岗位分类</text>
    </view>
    
    <scroll-view class="recruit-filter__scroll" scroll-x show-scrollbar="false">
      <view class="recruit-filter__items">
        <view 
          class="recruit-filter__item" 
          :class="{ 'recruit-filter__item--active': activeType === 0 }"
          @click="handleTypeChange(0)"
        >
          <text class="recruit-filter__item-text">全部</text>
        </view>
        <view 
          v-for="type in types" 
          :key="type.id"
          class="recruit-filter__item" 
          :class="{ 'recruit-filter__item--active': activeType === type.id }"
          @click="handleTypeChange(type.id)"
        >
          <text class="recruit-filter__item-text">{{ type.name }}</text>
        </view>
      </view>
    </scroll-view>
    
    <view class="recruit-filter__divider"></view>
    
    <view class="recruit-filter__header">
      <text class="recruit-filter__title">排序方式</text>
    </view>
    
    <view class="recruit-filter__sort">
      <view 
        class="recruit-filter__sort-item" 
        :class="{ 'recruit-filter__sort-item--active': sortBy === 'latest' }"
        @click="handleSortChange('latest')"
      >
        <text class="recruit-filter__sort-icon iconfont icon-time"></text>
        <text class="recruit-filter__sort-text">最新</text>
      </view>
      <view 
        class="recruit-filter__sort-item" 
        :class="{ 'recruit-filter__sort-item--active': sortBy === 'hot' }"
        @click="handleSortChange('hot')"
      >
        <text class="recruit-filter__sort-icon iconfont icon-fire"></text>
        <text class="recruit-filter__sort-text">热门</text>
      </view>
      <view 
        class="recruit-filter__sort-item" 
        :class="{ 'recruit-filter__sort-item--active': sortBy === 'deadline' }"
        @click="handleSortChange('deadline')"
      >
        <text class="recruit-filter__sort-icon iconfont icon-calendar"></text>
        <text class="recruit-filter__sort-text">截止日期</text>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue';

const props = defineProps({
  types: {
    type: Array,
    default: () => []
  },
  defaultType: {
    type: Number,
    default: 0
  },
  defaultSort: {
    type: String,
    default: 'latest'
  }
});

const emit = defineEmits(['filter-change']);

// 当前选中的类型
const activeType = ref(props.defaultType);

// 当前排序方式
const sortBy = ref(props.defaultSort);

// 切换类型
const handleTypeChange = (typeId) => {
  activeType.value = typeId;
  emitFilterChange();
};

// 切换排序方式
const handleSortChange = (sort) => {
  sortBy.value = sort;
  emitFilterChange();
};

// 触发筛选变化事件
const emitFilterChange = () => {
  emit('filter-change', {
    type: activeType.value,
    sort: sortBy.value
  });
};
</script>

<style lang="scss" scoped>
.recruit-filter {
  width: 100%;
  background-color: #ffffff;
  padding: 16px;
  box-sizing: border-box;
  border-radius: 16px;
  margin-bottom: 16px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.04);
  
  &__header {
    display: flex;
    align-items: center;
    margin-bottom: 12px;
  }
  
  &__title {
    font-size: 15px;
    font-weight: 600;
    color: #333;
    position: relative;
    padding-left: 10px;
    
    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 3px;
      height: 14px;
      background-color: #1890ff;
      border-radius: 1.5px;
    }
  }
  
  &__scroll {
    width: 100%;
    white-space: nowrap;
    margin-bottom: 16px;
  }
  
  &__items {
    display: inline-flex;
    padding: 0 4px;
  }
  
  &__item {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    height: 32px;
    padding: 0 16px;
    font-size: 14px;
    color: #666;
    background-color: #f5f7fa;
    border-radius: 16px;
    margin-right: 12px;
    transition: all 0.3s;
    
    &-text {
      line-height: 1;
    }
    
    &--active {
      background: linear-gradient(90deg, #1890ff, #36a6ff);
      color: #ffffff;
      box-shadow: 0 2px 8px rgba(24, 144, 255, 0.2);
    }
    
    &:last-child {
      margin-right: 0;
    }
    
    &:active {
      transform: scale(0.95);
    }
  }
  
  &__divider {
    width: 100%;
    height: 1px;
    background-color: #f0f2f5;
    margin: 8px 0 16px;
  }
  
  &__sort {
    display: flex;
    justify-content: space-between;
    padding: 0 8px;
  }
  
  &__sort-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 10px 0;
    position: relative;
    transition: all 0.3s;
    
    &-icon {
      font-size: 20px;
      color: #999;
      margin-bottom: 6px;
    }
    
    &-text {
      font-size: 13px;
      color: #666;
    }
    
    &--active {
      .recruit-filter__sort-icon,
      .recruit-filter__sort-text {
        color: #1890ff;
        font-weight: 500;
      }
      
      &::after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
        width: 20px;
        height: 3px;
        background-color: #1890ff;
        border-radius: 1.5px;
      }
    }
    
    &:active {
      transform: scale(0.95);
    }
  }
}
</style>
